﻿Imports Castle.ActiveRecord
Imports NHibernate
Imports NHibernate.Criterion

Namespace Model


    ''' <summary>
    '''	Criado em 21/08/2010 16:13:00
    ''' </summary>
    ''' <remarks></remarks>
    <Serializable(), ActiveRecord("estado", DynamicUpdate:=True)> _
    Public Class Estado
        Inherits ActiveRecordBase(Of Model.Estado)


        Private _cd_estado As Integer
        Private _estado_uf As String
        Private _estado_nome As String
        Private _estado_ibge As Decimal

        Private _Estados As IList(Of Model.Estado)
        Private _Municipios As IList(Of Model.Municipio)
        Private _Pessoas As IList(Of Model.Pessoa)
        Private _NFs As IList(Of Model.Nf)


        <PrimaryKey(PrimaryKeyType.Sequence, SequenceName:="estado_cd_estado_seq")> _
        Public Property cd_estado() As Integer
            Get
                Return _cd_estado
            End Get
            Set(ByVal value As Integer)
                _cd_estado = value
            End Set
        End Property

        <HasMany(GetType(Model.Municipio), Table:="estado", Lazy:=True, ColumnKey:="cd_estado")> _
        Public Property Estados() As IList(Of Model.Estado)
            Get
                Return _Estados
            End Get
            Set(ByVal value As IList(Of Model.Estado))
                _Estados = value
            End Set
        End Property

        <HasMany(GetType(Model.Municipio), Table:="municipio", Lazy:=True, ColumnKey:="cd_estado")> _
        Public Property Municipios() As IList(Of Model.Municipio)
            Get
                Return _Municipios
            End Get
            Set(ByVal value As IList(Of Model.Municipio))
                _Municipios = value
            End Set
        End Property

        <HasMany(GetType(Model.Pessoa), Table:="pessoa", Lazy:=True, ColumnKey:="cd_estado")> _
        Public Property Pessoas() As IList(Of Model.Pessoa)
            Get
                Return _Pessoas
            End Get
            Set(ByVal value As IList(Of Model.Pessoa))
                _Pessoas = value
            End Set
        End Property

        <HasMany(GetType(Model.Nf), Table:="nf", Lazy:=True, ColumnKey:="cd_estado")> _
        Public Property NFs() As IList(Of Model.Nf)
            Get
                Return _NFs
            End Get
            Set(ByVal value As IList(Of Model.Nf))
                _NFs = value
            End Set
        End Property


        <[Property]("estado_uf")> _
        Public Property estado_uf() As String
            Get
                Return _estado_uf
            End Get
            Set(ByVal value As String)
                _estado_uf = value
            End Set
        End Property

        <[Property]("estado_nome")> _
        Public Property estado_nome() As String
            Get
                Return _estado_nome
            End Get
            Set(ByVal value As String)
                _estado_nome = value
            End Set
        End Property

        <[Property]("estado_ibge")> _
        Public Property estado_ibge() As Decimal
            Get
                Return _estado_ibge
            End Get
            Set(ByVal value As Decimal)
                _estado_ibge = value
            End Set
        End Property

        Public Function GetByIBGE(ByVal Ibge As Integer) As Model.Estado
            Dim q As DetachedCriteria = DetachedCriteria.For(Of Model.Estado)("estado")

            q.Add(Restrictions.Eq("estado_ibge", CType(Ibge, Decimal)))

            Return Model.Estado.FindFirst(q)
        End Function

        Public Function ListOrderByFild(ByVal field As String) As IList(Of Model.Estado)

            Dim Ordem As NHibernate.Criterion.Order

            Ordem = NHibernate.Criterion.Order.Asc(field)

            Return Model.Estado.FindAll(Ordem)
        End Function

    End Class
End Namespace
